package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.utils.DateUtil;
import com.neusoft.neuiotms.dcom.model.OperationSupportWeekly;
import com.neusoft.neuiotms.dcom.model.QOperationSupportWeekly;
import com.neusoft.neuiotms.dcom.repository.CustomizedOperationSupportWeeklyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedOperationSupportWeeklyRepositoryImpl extends AbstractRepository
        implements CustomizedOperationSupportWeeklyRepository {

    @Override
    public List<OperationSupportWeekly> index(final Searchable searchable) {
        final QOperationSupportWeekly qOperationSupportWeekly = QOperationSupportWeekly.operationSupportWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<OperationSupportWeekly> query = this.query().selectFrom(qOperationSupportWeekly);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<OperationSupportWeekly> search(final Pageable pageable, final Searchable searchable) {
        final QOperationSupportWeekly qOperationSupportWeekly = QOperationSupportWeekly.operationSupportWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<OperationSupportWeekly> query = this.query().selectFrom(qOperationSupportWeekly);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return OperationSupportWeekly.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QOperationSupportWeekly qOperationSupportWeekly = QOperationSupportWeekly.operationSupportWeekly;

        if (!StringUtils.isEmpty(searchable.getStrValue("companyCode"))) {
            where.and(super.equalsStr(qOperationSupportWeekly.companyCode, searchable, "companyCode"));
        }

        // 单位名称检索
        if (!StringUtils.isEmpty(searchable.getStrValue("companyName"))) {
            where.and(super.containsStr(qOperationSupportWeekly.companyName, searchable, "companyName"));
        }

        if (null != searchable.getStrArray("institutionCodeArray")) {
            where.and(super.inStrings(qOperationSupportWeekly.companyCode, searchable, "institutionCodeArray"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("userCode"))) {
            where.and(super.equalsStr(qOperationSupportWeekly.userCode, searchable, "userCode"));
        }

        if (null != searchable.getLongValue("weekDate")) {
            final long weekTime = searchable.getLongValue("weekDate");
            final Date startTimeOfWeek = DateUtil.getStartTimeOfWeek(new Date(weekTime));
            final Date endTimeOfWeek = DateUtil.getEndTimeOfWeek(new Date(weekTime));
            where.and(qOperationSupportWeekly.weekDate.before(endTimeOfWeek));
            where.and(qOperationSupportWeekly.weekDate.goe(startTimeOfWeek));
        }

        if (null != searchable.getLongValue("afEqReportDate")) {
            where.and(super.afterOrEqualsDate(qOperationSupportWeekly.weekDate, searchable, "afEqReportDate"));
        }

        if (null != searchable.getLongValue("beEqReportDate")) {
            where.and(super.beforeOrEqualsDate(qOperationSupportWeekly.weekDate, searchable, "beEqReportDate"));
        }

        return where;
    }
}
